package com.ngu.sys.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ngu.sys.user.dao.TSysUserMapper;
import com.ngu.sys.user.model.TSysUser;

import com.ngu.sys.user.service.TSysUserService;
import com.ngu.util.AjaxResponse;
import com.ngu.util.IpUtil;
import com.ngu.util.MD5Util;
import com.ngu.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

import static com.ngu.util.TimeUtils.getNowTime;

/**
 * @description: 系统用户接口实现层
 * @author: HSJ
 * @create: 2020-03-01 13:27
 **/
@Service
public class TSysUserServiceImpl extends ServiceImpl<TSysUserMapper, TSysUser> implements TSysUserService  {
    @Autowired
    private TSysUserMapper tSysUserMapper;

    /**
    * @MethodDescription:    登录
    * @Author:      hsj
    * @CreateDate:       2020/3/4 15:33
    */
    @Override
    public AjaxResponse login(TSysUser T, HttpSession session) {
        //将密码md5加密查询登录
        T.setPassword(MD5Util.getMD5Str(T.getPassword()));

        List<TSysUser> tSysUser = 	tSysUserMapper.selectByUNameOrPwd(T);
        if(ObjectUtils.isEmpty(tSysUser)){
            return  AjaxResponse.error("账号或密码错误！",tSysUser);
        }else{
            //修改最后登录时间和登录ip地址
            tSysUser.get(0).setLastLoginIp(IpUtil.getLocalIpAddr());
            tSysUser.get(0).setLastLoginTime(TimeUtils.getNowTime());
            tSysUserMapper.updateById(tSysUser.get(0));
            //隐藏密码
            tSysUser.get(0).setPassword("**密码被隐藏**");
            session.setAttribute("SysUser",tSysUser);
            return  AjaxResponse.success("登录成功！",tSysUser);
        }
    }



    /**
    * @MethodDescription:    新增
    * @Author:      hsj
    * @CreateDate:       2020/3/1 16:41
    */
    @Override
    public Integer insertSysUser(TSysUser T) {
       T.setCreateTime(getNowTime());
       return tSysUserMapper.insert(T);


    }

    /**
    * @MethodDescription:    修改
    * @Author:      hsj
    * @CreateDate:       2020/3/1 16:41
    */
    @Override
    public Integer updateSysUser(TSysUser T) {
        return  tSysUserMapper.updateById(T);

    }

    /**
    * @MethodDescription:    配置了逻辑删除
    * @Author:      hsj
    * @CreateDate:       2020/3/1 16:22
    */
    @Override
    public Integer deleteById(Long id) {
        return tSysUserMapper.deleteById(id);

    }


}
